package com.example

import java.sql
import java.sql.PreparedStatement

import com.alibaba.druid.pool.{DruidDataSource, DruidPooledConnection}

object Test {
  private val dataSource:DruidDataSource = new DruidDataSource()
  var connection:DruidPooledConnection =_
  var statement:sql.Statement = _
  var preparedStatement: PreparedStatement = _

  def main(args: Array[String]): Unit = {
    getConnection()
  }

  def getConnection() = {
    dataSource.setUrl("jdbc:mysql://linux123:3306/test?useSSL=false");
    dataSource.setUsername("root");
    dataSource.setPassword("12345678");

    //下面都是可选的配置
    dataSource.setInitialSize(10);  //初始连接数，默认0
    dataSource.setMaxActive(30);  //最大连接数，默认8
    dataSource.setMinIdle(10);  //最小闲置数
    dataSource.setMaxWait(2000);  //获取连接的最大等待时间，单位毫秒
    dataSource.setPoolPreparedStatements(true); //缓存PreparedStatement，默认false
    dataSource.setMaxOpenPreparedStatements(20); //缓存PreparedStatement的最大数量，默认-1（不缓存）。大于0时会自动开启缓存PreparedStatement，所以可以省略上一句代码

    //获取连接
    connection = dataSource.getConnection()
    statement = connection.createStatement()
//    val sql1 = """insert into businfo values ("adfgfdewr-5463243546-4c68-9d24-8a7e8","300")"""
//    val res: Int = statement.executeUpdate(sql1)
//    if (res > 0) println("成功")
//    else println("失败")

//    val sql = "update businfo set lglat = ? where deployNum = ?;"
    val sql2 = "update businfo set lglat=? where deployNum=?;"
    preparedStatement = connection.prepareStatement(sql2)
    preparedStatement.setString(1,"kkkk")
    preparedStatement.setString(2,"316d5c75-e860-4cc9-a7de-ea2148c244a0")
    val result: Int = preparedStatement.executeUpdate()
    if (result > 0) println("修改成功")
    else println("修改失败")
    preparedStatement.close()
//    statement.close()
    connection.close()

  }

}
